package com.hm.service.controller.chat;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hm.service.framework.request.post.PostBase;
import com.hm.service.framework.request.post.PostHandleBean;
import com.hm.service.framework.response.ResponseConstant;
import com.hm.service.service.filtration.IFiltrationService;
import com.hm.service.service.spark.count.IRuleService;
import com.hm.service.service.spark.model.IBigModelService;
import com.hm.service.service.token.ITokenService;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping(value = "/Chat", produces = {"application/json;charset=UTF-8"})
public class CFSS_000_000_003 extends PostBase {
    private static final Logger logger = LoggerFactory.getLogger(CFSS_000_000_003.class);

    private final IBigModelService bigModelService;
    private final IFiltrationService filtrationService;
    private final ITokenService tokenService;
    private final IRuleService ruleService;

    public CFSS_000_000_003(IBigModelService iBigModelService, ITokenService iTokenService, IFiltrationService iFiltrationService, IRuleService iRuleService) {
        this.bigModelService = iBigModelService;
        this.tokenService = iTokenService;
        this.filtrationService = iFiltrationService;
        this.ruleService = iRuleService;
    }

    //对话接口
    @PostMapping(value = "/000_000_003")
    public String doPost(HttpServletRequest request) {
        return handleRequest(request);
    }

    @Override
    protected String postRequest(PostHandleBean reqBean) {
        //1、校验token
        JSONObject jsonObject = (JSONObject) JSON.parse(reqBean.getReqBody());
        String token = jsonObject.getString("token");
        HashMap<String, String> verify = tokenService.verify(token);
        if (!verify.get("state").equals("0")) {
            return errorResponse(ResponseConstant.PARAMETER_ERROR_CODE, verify.get("msg"), "");
        }
        //非空判断
        String problem = jsonObject.getString("problem");
        if (problem == null || problem.isEmpty()) {
            return successResponse(reqBean.getRealSecretKey(), ResponseConstant.SUCCESS_CODE, "查询成功", "问题不能为空喔~");
        }
        //2、规则拦截
        String userId = tokenService.getUserId(token);
        String difference = ruleService.timeDifference(userId);
        if (difference.equals("0")) {
            //触发了拦截
            return successResponse(reqBean.getRealSecretKey(), ResponseConstant.SUCCESS_CODE, "查询成功", "你问题有点频繁喔");
        }
        //3、关键字拦截
        String interceptResult = filtrationService.interceptKey(userId, problem);
        if (interceptResult.equals("0")) {
            //触发了拦截
            return successResponse(reqBean.getRealSecretKey(), ResponseConstant.SUCCESS_CODE, "查询成功", "你触发了违禁关键字喔");
        }
        //3、对话逻辑-》正式环境应该接入AI
        ruleService.setTag(userId);
        String reply = bigModelService.question(problem);
        if (reply == null || reply.isEmpty()) {
            //触发了ai的关键字，会返回空
            return successResponse(reqBean.getRealSecretKey(), ResponseConstant.SUCCESS_CODE, "查询成功", "请正确输入问题喔~例如：今天清远天气怎样？");
        }
        return successResponse(reqBean.getRealSecretKey(), ResponseConstant.SUCCESS_CODE, "查询成功", reply);
    }

}
